<?php
class LoginController extends AppController {

	var $name = 'Login';
	var $uses = array('User','Order');
	var $helpers = array('Html', 'Form');
    var $components = array('Auth');
    function beforeFilter(){
		parent::beforeFilter();
		$this->set('user',$this->Auth->user());
    	// 暫定
    	//$this->Auth->allow('*');
    }
    function admin_list_users(){
    	$this->pageTitle = 'ユーザ一覧';
    	$this->set('lists',$this->User->find('all'));
    }
    function admin_delete_user($id){
    	$rslt = $this->User->find('first',array('conditions' => array('User.id' => $id)));
    	//debug($rslt);
    	try{
    		$this->User->del($rslt['User']['id']);
    		$this->Session->setFlash(sprintf("ユーザ %s が削除されました",$rslt['User']['name']));
    	}catch(Exception $e){
    		$this->Session->setFlash($e->getMessage());
    	}
    	$this->redirect(array('action' => 'list_users'));
    }
    function admin_index(){
    	$this->pageTitle = 'ストア管理';
    	$this->set('total_orders',$this->Order->count());
    	$this->set('pending_orders',$this->Order->count_pending());
    }
	function admin_login(){
	}
	function admin_logout() {
		$this->Session->setFlash('ログアウトしました');
		$this->redirect($this->Auth->logout());
	}
	function admin_add_user(){
		$this->pageTitle = 'ユーザの追加';
		if (empty($this->data)){
			// フォームを表示
		}else{
			$passwd = $this->Auth->password($this->data['User']['password']);
			$this->data['User']['hashed_password'] = $passwd;
			$username = $this->data['User']['name'];
			//debug($this->data);
			if( $this->User->save($this->data) ){
                $this->Session->setFlash("ユーザ $username が作成されました");
                #$this->redirect(array('action' => 'admin_index'));
			}else{
				$this->Session->setFlash("ユーザの追加に失敗しました");
			}
		}
	}
}
?>